#include "TestSql.h"
#include "ui_TestSql.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>

TestSql::TestSql(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestSql)
{
    ui->setupUi(this);
    connect(ui->btnSubmit, SIGNAL(clicked()), this, SLOT(onSubmitClicked()));
    connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(onCancelClicked()));
}

TestSql::~TestSql()
{
    delete ui;
}

void TestSql::onSubmitClicked()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("myTestDb");
    db.setUserName(ui->txtName->text());
    db.setPassword(ui->txtPasswd->text());
    bool ok = db.open();
    if (ok) {
        QMessageBox::information(this, "succeed", "connect succeed!");
    }
    else {
        QMessageBox::information(this, "failed", "connect failed!" + db.lastError().text());
    }
}

void TestSql::onCancelClicked()
{
    this->close();
}
